/*=====================
 * 功能: 基础算法组件
 * 作者: zjsina-pp
 * 版本: 0.1.0
 * 创建时间: 2014-9-19
 * 更新时间: 2014-9-19
 * more: http://ppstudio.sinaapp.com/
 * 注： 调用请注明出处  谢谢
 ======================*/
(function(global, undefined) {
	/* *
	 *模块1：列表
	 *
	 */
	var List = function(array) {
		this.dataStore = array || [];
		this.listSize = this.dataStore.length;
		this.pos = 0;
	}
	List.prototype = {
		append : function(element) {
			this.dataStore[this.listSize++] = element;
		},
		find : function(element) {
			for (var i = 0, l = this.dataStore.length; i < l; i++) {
				if (this.dataStore[i] == element) {
					return i;
				}
			}
			return -1;
		},
		remove : function(element) {
			var foundAt = this.find(element);
			if (foundAt > -1) {
				this.dataStore.splice(foundAt, 1);
				this.listSize--;
				return true;
			}
			return false;
		},
		length : function() {
			return this.listSize;
		},
		toString : function() {
			return this.dataStore;
		},
		insert : function(element, after) {
			var insertPos = this.find(after);
			if (insertPos > -1) {
				this.dataStore.splice(insertPos + 1, 0, element);
				this.listSize++;
				return true;
			}
			return false;
		},
		clear : function() {
			delete this.dataStore;
			this.dataStore = [];
			this.listSize = this.pos = 0;
		},
		/*定位操作*/
		front : function() {
			this.pos = 0;
		},
		end : function() {
			this.pos = this.listSize - 1;
		},
		prev : function() {
			if (this.pos > 0) {
				this.pos--;
			}
		},
		next : function() {
			if (this.pos < this.listSize - 1) {
				this.pos++;
			}
		},
		currPos : function() {
			return this.pos;
		},
		moveTo : function(position) {
			this.pos = position;
		},
		getElement : function() {
			return this.dataStore[this.pos];
		}
	}
	global.List = List;
	/* *
	 *模块2：栈
	 *
	 */
	var Stack = function() {
		this.dataStore = [];
		this.top = 0;
	}
	Stack.prototype = {
		push : function(element) {
			this.dataStore[this.top++] = element;
		},
		peek : function() {
			return this.dataStore[this.top - 1];
		},
		pop : function() {
			return this.dataStore[--this.top];
		},
		clear : function() {
			this.top = 0;
			this.dataStore = [];
		},
		length : function() {
			return this.top;
		}
	}
	global.Stack = Stack;
	/* *
	 *模块3：队列
	 *
	 */
	var Queue = function() {
		this.dataStore = [];
	}
	Queue.prototype = {
		enqueue : function(element) {m
			this.dataStore.push(element);
		},
		dequeue : function() {
			return this.dataStore.shift();
		},
		front : function() {
			return this.dataStore[0]
		},
		back : function() {
			return this.dataStore[this.dataStore.length - 1];
		},
		toString : function() {
			var reStr = '';
			for (var i = 0, l = this.dataStore.length; i < l; i++) {
				reStr += this.dataStore[i] + '\n';
			}
			return reStr;
		},
		empty : function() {
			return this.dataStore.length == 0 ? true : false;
		}
	}
	global.Queue = Queue;
})(this)
